package com.example.huanhang.changzhouapp.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.allen.library.SuperTextView;
import com.example.huanhang.changzhouapp.R;
import com.example.huanhang.changzhouapp.base.BaseBackActivity;
import com.gyf.barlibrary.ImmersionBar;
import com.tencent.bugly.beta.Beta;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lei
 * @desc 关于页面
 * @date 2018/9/6 0006 -- 上午 10:06.
 * 个人博客站: http://www.bestlei.top
 */

public class AboutActivity extends BaseBackActivity implements SuperTextView.OnSuperTextViewClickListener {

    @BindView(R.id.about_update_log)
    SuperTextView mAboutUpdateLog;
    @BindView(R.id.about_contact_us)
    SuperTextView mAboutContactUs;
    @BindView(R.id.about_check)
    SuperTextView mAboutUpdate;
    @BindView(R.id.fankui)
    SuperTextView mFanKui;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    public int getLayoutId() {
        return R.layout.activity_about;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        setTitle("关于");
        ImmersionBar.with(this).statusBarColor(R.color.title).init();
        sp = getSharedPreferences("isCheck", MODE_PRIVATE);
        editor = sp.edit();
        if (sp.getBoolean("check", false) == true) {
            mAboutUpdateLog.setSwitchIsChecked(true);
            mAboutUpdateLog.setLeftBottomString(getString(R.string.OFF_BUTTON));
        } else {
            mAboutUpdateLog.setSwitchIsChecked(false);
            mAboutUpdateLog.setLeftBottomString(getString(R.string.OPEN_BUTTON));
        }
        mAboutUpdate.setOnSuperTextViewClickListener(this);
        mFanKui.setOnSuperTextViewClickListener(this);
    }

    @Override
    public void initData() {
        mAboutUpdateLog.setSwitchCheckedChangeListener((button, b) -> {
            if (b) {
                editor.putBoolean("check", b).apply();
                mAboutUpdateLog.setLeftBottomString(getString(R.string.OFF_BUTTON));
            } else {
                editor.putBoolean("check", b).apply();
                mAboutUpdateLog.setLeftBottomString(getString(R.string.OPEN_BUTTON));
            }
        });
        mAboutUpdate.setRightString(getString(R.string.app_version_text) + getVersion());
        //反馈页面不支持低于5.0
        if (Build.VERSION.SDK_INT >= 21) {
            mFanKui.setVisibility(View.VISIBLE);
        } else {
            mFanKui.setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.about_contact_us)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.about_contact_us:
                Intent intent = new Intent(this, ContantUsActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onClickListener(SuperTextView view) {
        switch (view.getId()) {
            case R.id.about_check:
                Beta.checkUpgrade();
                break;
            case R.id.fankui:
                Intent intent = new Intent(AboutActivity.this, FanKuiActivity.class);
                startActivity(intent);
                break;
        }

    }
}
